package com.example.module.custom.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.module.custom.R;
import com.example.module.custom.R2;
import com.example.module.custom.bean.PQCDetailsBean;
import com.example.module.custom.fragment.PQCInforFragment;
import com.example.module.custom.fragment.PQCTestFragment;
import com.example.module.custom.fragment.PQCTestFragment2;
import com.example.module.custom.http.HTTPURL;
import com.example.module.custom.utils.ToastUtil;
import com.google.android.material.tabs.TabLayout;
import com.nand.library.base.base.BaseActivity;
import com.nand.library.zxing.activity.CodeUtils;


import java.util.ArrayList;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @name BlockDemo
 * @class name：com.nand.module.function.ui
 * @classdescribe PQC模组新增
 * @anthor ljp
 * @time 2020/1/7 10:23
 */
@Route(path = "/custom/PQCAdd")
public class PQCAddActivity extends BaseActivity {
    @BindView(R2.id.tv_headline)
    TextView tvHeadline;
    @BindView(R2.id.svpi_pager)
    TabLayout svpiPager;
    @BindView(R2.id.pager_pqc_add)
    ViewPager pagerPqcAdd;
    private ArrayList<Fragment> fragments;
    private String[] mTitles;
    private MAdapter mAdapter;
    @Autowired(name = "oid")
    public String oid;
    @Autowired(name = "flag")
    public int flag;
    PQCDetailsBean pqcDetailsBean;
    @Override
    protected int initLayout() {
        return R.layout.activity_pqc_add;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        mTitles = getResources().getStringArray(R.array.text);
        svpiPager.setTabMode(TabLayout.MODE_FIXED);
        //让viewpager和顶部标题关联
        svpiPager.setupWithViewPager(pagerPqcAdd);
        initViewPager();
        pagerPqcAdd.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(position == 1){
                   // Log.i(TAG, "onPageScrolled: -------"+pqcDetailsBean.toString());
                }
            }
            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {
        switch (flag){
            case 1:
                tvHeadline.setText("新增PQC");
                break;
            case 2:
                tvHeadline.setText("PQC复查");
                break;
            case 3:
                tvHeadline.setText("PQC详情");
                break;
        }
    }
    /**
     *  @author ljp
     *  @time   13:18
     *  @describe fragment切换
     */
    public void setFragments(String string,boolean is){
        pagerPqcAdd.setCurrentItem(0);
        ((PQCInforFragment) fragments.get(0)).setJson(string,is);
    }
    /**
     * @return
     * @method
     * @description viewpager与fragment绑定
     * @date: 2018/10/11 16:40
     * @author: ljp
     */
    private void initViewPager() {
        fragments = new ArrayList<>();
        fragments.add(PQCInforFragment.newInstance(flag,oid));
        fragments.add(PQCTestFragment2.newInstance(flag,oid));
        mAdapter = new MAdapter(getSupportFragmentManager());
        pagerPqcAdd.setAdapter(mAdapter);
    }


    @OnClick(R2.id.real_back)
    public void onViewClicked() {
      //  ARouter.getInstance().build("/custom/PQCList").withInt("type", flag).navigation();
        finish();
    }

    private class MAdapter extends FragmentPagerAdapter {
        public MAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        //一定要重写这个返回标题的方法;
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
